﻿using System;
using EOSCRM.Dao;
using EOSCRM.Util;
using EOSCRM.Web.Common;
using EOSCRM.Web.Shared;
using System.Web.UI.WebControls;
using EOSCRM.Domain;

namespace EOSCRM.Web.Forms.HeThong
{
    public partial class ChangePasswordPerson : Authentication
    {
        private readonly UserAdminDao _objDao = new UserAdminDao();


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                    LoadReferences();
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        private void LoadReferences()
        {
            if (LoginInfo == null) return;
            lblUSERNAME.Text = "";
            upnlCustomers.Update();
            BindData();
        }
        protected void BindData()
        {
            var list = _objDao.GetList(null, txttenNhanvien.Text.Trim());
            gvNguoiDung.DataSource = list;
            gvNguoiDung.DataBind();
            upnlGrid.Update();
        }
        
        private void ClearForm()
        {
            LoadReferences();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if(txtPASSWORD.Text.Trim() == "")
            {
                CloseWaitingDialog();
                ShowError("Mật khẩu không hợp lệ.", txtPASSWORD.ClientID);
                return;
            }

            if (CurentUser == null)
            {
                ShowError("Bạn chưa chọn người dùng cần đổi mật khẩu");
                CloseWaitingDialog();
                return;
            }

            var useradmin = _objDao.Get(CurentUser.Id);
            if (useradmin == null)
            {
                CloseWaitingDialog();
                return;
            }

            useradmin.Password = txtPASSWORD.Text.Trim();
            useradmin.UpdateBy = LoginInfo.Username;
            useradmin.UpdateDate = DateTime.Now;

            var msg = _objDao.Update(useradmin);

            CloseWaitingDialog();

            if(msg != null)
            {
                if(msg.MsgType.Equals(MessageType.Error))
                {
                    ShowError(ResourceLabel.Get(msg));
                }
                else
                {
                    ShowInfor(ResourceLabel.Get(msg));
                    ClearForm();
                }
            }
        }
        protected UserAdmin CurentUser
        {
            get
            {
                return Session["UserChosen"] as UserAdmin;
            }
            set
            {
                value = (UserAdmin)Session["UserChosen"];
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
            CloseWaitingDialog();
        }
        protected void gvNguoiDung_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                var id = e.CommandArgument.ToString();

                switch (e.CommandName)
                {
                    case "EditUser":
                        var user = _objDao.Get(null, Int32.Parse(id));

                        if (user != null)
                        {
                            SetControlValue(hdId.ClientID, user.Id.ToString());
                            SetLabel(lblUSERNAME.ClientID, user.Username);
                            SetControlValue(txttenNhanvien.ClientID, user.NHANVIEN.TenNV);
                            Session["UserChosen"] = user;
                            
                            upnlCustomers.Update();
                            CloseWaitingDialog();
                        }
                        else
                        {
                            CloseWaitingDialog();
                            ShowInfor("Người dùng không tồn tại.");
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            BindData();
            CloseWaitingDialog();
        }
    }
}